<template>
  <base-layout>
    <template #searchPanel>
      <search-panel
        v-slot="{ form }"
        :addPer="'ygfl:save'"
        :selectPer="'ygfl:page'"
        addTitle="新增"
        @insert="add"
        @search="search"
        :showSearchButton="true"
        :showAddButton="true"
      >
        <el-select
          clearable
          style="width: 150px"
          v-model="form.typeDictId"
          placeholder="请选择返利类型"
        >
          <el-option
            v-for="item in getDict(['预估返利类型'])['预估返利类型']"
            :key="item.index"
            :value="item.value"
            :label="item.text"
          />
        </el-select>
        <el-select
          clearable
          style="width: 150px"
          v-model="form.carBrandId"
          placeholder="请选择品牌"
          @change="handleCarBrand"
        >
          <el-option
            v-for="item in allBrand"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
        <el-select
          clearable
          style="width: 150px"
          v-model="form.seriesId"
          placeholder="请选择车系"
        >
          <el-option
            v-for="item in allSeries"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
        <el-select
          clearable
          style="width: 150px"
          v-model="form.status"
          placeholder="状态"
        >
          <el-option
            v-for="item in getDict(['返利审核状态'])['返利审核状态']"
            :key="item.index"
            :value="item.value"
            :label="item.text"
          />
        </el-select>
        <el-date-picker
          v-model="form.paymentTime"
          type="datetimerange"
          start-placeholder="登记日期"
          end-placeholder="登记日期"
          clearable
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
        />
        <el-input
          v-model="form.code"
          clearable
          placeholder="请输入返利单号"
          style="width: 150px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="160"
      >
        <template #action="{ row }">
          <i
            class="el-icon-edit"
            title="修改"
            style="margin-left: 15px; cursor: pointer"
            v-permission="'ygfl:update'"
            @click="updateUser(row)"
          ></i>
          <i
            class="el-icon-delete"
            title="删除"
            style="color: red; margin-left: 20px; cursor: pointer"
            v-permission="'ygfl:del'"
            @click="handleDataDelete(row)"
          ></i>
        </template>
      </data-table>
    </template>
  </base-layout>
</template>
<script>
import baseMixin from '@/mixins/base'
import {
  getList,
  deleteById,
  update
} from '@/api/pre-sale/newCarRebate'
import { getDict } from '@/utils/index'
import {
  getAllBrand,
  getSeries
} from '@/api/after-sale/systemManage/carModel'

export default {
  mixins: [
    baseMixin({
      idName: 'id',
      findData: getList,
      updateData: update,
      deleteData: deleteById,
      dataName: ['content', 'totalRecords'],
      format(res) {
        return res.map((data) => {
          data.icxfl = data.icxfl === true ? '是' : '否'
          data.zzs = (data.zzs * 100) + '%'
          data.brandSeriesModel = data.carBrandName + '/' + data.seriesName
          return data
        })
      }
    })
  ],
  data() {
    return {
      allBrand: [], // 品牌
      allSeries: [], // 车系
      getDict: getDict,
      form: {
        client: '' // 客户信息
      },
      tableColumnAttributes: [
        { name: '门店', value: 'storeName', type: 'text' },
        { name: '返利单号', value: 'code', type: 'text' },
        { name: '品牌/车系', value: 'brandSeriesModel', type: 'text' },
        { name: '返利年月', value: 'rebateMonth', type: 'text' },
        { name: '返利类型', value: 'typeDictValue', type: 'text' },
        { name: '未确认进口车', value: 'wqrjkc', type: 'text' },
        { name: '未确认国产车', value: 'wsrgcc', type: 'text' },
        { name: '是否为一次性返利', value: 'icxfl', type: 'text' },
        { name: '总金额', value: 'amount', type: 'text' },
        { name: '税后金额', value: 'afterTaxAmount', type: 'text' },
        { name: '税额', value: 'taxAmount', type: 'text' },
        { name: '增值税率', value: 'zzs', type: 'text' },
        { name: '登记日期', value: 'createTime', type: 'text' },
        { name: '备注', value: 'remark', type: 'text' },
        { name: '状态', value: 'stauts', type: 'text' }
      ],
      dict: {}
    }
  },
  methods: {
    async handleCarBrand(val) {
      val && await getSeries(val).then((res) => {
        const { success, data } = res
        if (success) this.allSeries = data
      })
    },
    // 编辑信息
    updateUser(row) {
      this.$router.push({
        path: 'rebateInformation',
        query: {
          id: row.id
        }
      })
    },
    // 查询
    search(form) {
      if (form.paymentTime && form.paymentTime.length > 0) {
        form.startTime = form.paymentTime[0]
        form.endTime = form.paymentTime[1]
      } else {
        form.startTime = ''
        form.endTime = ''
      }
      delete form.paymentTime
      this.searchOptions = form
      this.currentPage = 1
      this.setTableData()
    },
    // 新增信息
    add() {
      this.$router.push({
        name: 'rebateInformation',
        query: { type: 'add' }
      })
    }
  },
  async mounted() {
    await getAllBrand().then((res) => {
      const { success, data } = res
      if (success) this.allBrand = data
    })
  }
}
</script>
